package com.ficus.road.maintain.handler.smartdecision.interfaces;

import com.ficus.road.maintain.core.model.smartnew.SmartDecisionExcelVO;
import com.ficus.road.maintain.core.model.smartnew.SmartTable;
import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/8/1 下午5:06
 */
public interface SmartDecisionGroupGenerator {

    Function<SmartTable, String> SORTING_GROUP_RULE = table -> {
        Joiner joiner = Joiner.on("_");
        return joiner.join(table.getRoadName(), table.getDirection());
    };

    Function<SmartTable, String> VALUATION_GROUP_RULE = SmartDecisionExcelVO::getRoadName;

    /**
     * 生成groupId
     *
     * @param smartTables
     * @param groupRules
     * @return
     */
    default List<SmartTable> generateGroupId(List<SmartTable> smartTables, Function<SmartTable, String> groupRules) {
        List<SmartTable> res = new ArrayList<>();
        Map<String, List<SmartTable>> map = smartTables.stream().collect(Collectors.groupingBy(groupRules));
        int groupId = 0;
        for (String roadName : map.keySet()) {
            List<SmartTable> highwayRoadList = map.get(roadName);
            highwayRoadList.sort(Comparator.comparing(SmartDecisionExcelVO::getStartStake));
            Double startStake = highwayRoadList.get(0).getStartStake();
            Double endStake = highwayRoadList.get(0).getEndStake();
            for (SmartTable road : highwayRoadList) {
                if (!road.getStartStake().equals(startStake) && !road.getStartStake().equals(endStake)) {
                    groupId++;
                }
                startStake = road.getStartStake();
                endStake = road.getEndStake();
                road.setGroupId(groupId);
                res.add(road);
            }
            groupId++;
        }
        return res;
    }
}
